package com.md.application.assembler;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.md.base.exception.ServerException;
import com.md.domain.model.bo.QueryAppInfoBO;
import com.md.domain.model.po.AppInfoPO;
import com.md.domain.model.po.ModuleAppPO;
import com.md.infrastructure.value.ErrorEnum;
import com.md.model.page.PageResponse;
import com.md.request.AppInfoRequest;
import com.md.request.QueryAppInfoRequest;
import com.md.response.AppInfoResponse;
import com.md.util.ClassUtils;
import com.md.util.DateUtils;
import com.md.util.EncryptionUtils;
import com.md.util.ObjectUtil;
import com.md.util.StringUtil;
import com.md.value.AppTypeEnum;
import lombok.experimental.UtilityClass;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author 刘骄阳
 * @date 2024/7/14 22:22
 **/
@UtilityClass
public class AppInfoAssembler {
  public static void checkParamValue(final Long id,
                                     final Long moduleId,
                                     final String modelName,
                                     final Long tenantId,
                                     final Long brandId) {
    if (ObjectUtil.isAllEmpty(id, moduleId, modelName, tenantId, brandId)) {
      throw ServerException.of(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
  }

  public static AppInfoPO buildAppInfoPO(final QueryAppInfoBO queryBO) {
    return ClassUtils.source2Target(queryBO, AppInfoPO.class);
  }

  public static List<AppInfoResponse> buildAppInfoPO2Response(final List<AppInfoPO> list) {
    return ClassUtils.source2Target(list, AppInfoResponse.class, (s, t) ->
      t.setAppExtra(StringUtil.jsonToMap(s.getAppExtra())));
  }

  public static PageResponse<AppInfoResponse> buildAppInfoPO2Response(final IPage<AppInfoPO> appInfoPOIPage) {
    return PageResponse.of(appInfoPOIPage.getPages(), appInfoPOIPage.getTotal(), appInfoPOIPage.getCurrent(),
      appInfoPOIPage.getSize(), ClassUtils.source2Target(appInfoPOIPage.getRecords(), AppInfoResponse.class,
        (s, t) -> {
          t.setAppExtra(StringUtil.jsonToMap(s.getAppExtra()));
          t.setAppType(AppTypeEnum.of(s.getAppType()));
        }));
  }

  public static AppInfoPO buildAppInfoRequest2PO(final AppInfoRequest appInfoRequest) {
    return ClassUtils.source2Target(appInfoRequest, AppInfoPO.class, target -> {
      target.setAppExtra(StringUtil.objectToJson(Optional.ofNullable(appInfoRequest.getAppExtra())
        .orElse(Maps.newHashMap())));
      target.setAppType(appInfoRequest.getAppType().getCode());
    });
  }

  public static AppInfoResponse buildAppInfoPO2Response(final AppInfoPO appInfoPO) {
    return ClassUtils.source2Target(appInfoPO, AppInfoResponse.class, t -> {
      Optional.ofNullable(appInfoPO.getAppExtra()).ifPresent(extra ->
        t.setAppExtra(StringUtil.jsonToMap(extra)));
      t.setAppType(AppTypeEnum.of(appInfoPO.getAppType()));
    });
  }

  public static QueryAppInfoBO buildQueryAppInfoBO(final QueryAppInfoRequest request) {
    return ClassUtils.source2Target(request, QueryAppInfoBO.class);
  }

  public static String getAppSecret(final String appCode) {
    return EncryptionUtils.aesEncryption(UUID.randomUUID() + DateUtils.format(LocalDateTime.now()), appCode);
  }

  public static List<ModuleAppPO> buildModuleLinkList(final Long moduleId, final List<Long> appInfoIds) {
    return appInfoIds.stream().map(appInfoId -> {
      ModuleAppPO moduleAppPO = new ModuleAppPO();
      moduleAppPO.setAppInfoId(appInfoId);
      moduleAppPO.setModuleId(moduleId);
      return moduleAppPO;
    }).collect(Collectors.toList());
  }
}
